package org.example.com.mapper;

import org.apache.ibatis.annotations.*;
import org.example.com.pojo.User;

@Mapper
public interface UserMapper {

    // 根据手机号查用户（用于注册）
    @Select("SELECT id, phone, password, name, avatar, created_at, updated_at " +
            "FROM users WHERE phone = #{phone}")
    User findByPhone(String phone);

    // 根据手机号或姓名查用户（用于登录）
    @Select("SELECT id, phone, password, name, avatar, created_at, updated_at " +
            "FROM users WHERE phone = #{loginKey} OR name = #{loginKey} LIMIT 1")
    User findByPhoneOrName(String loginKey);

    // 插入新用户
    @Insert("INSERT INTO users (phone, password, name, avatar, created_at, updated_at) " +
            "VALUES (#{phone}, #{password}, #{name}, #{avatar}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);

    // 根据用户ID查询用户
    @Select("SELECT id, phone, password, name, avatar, created_at, updated_at " +
            "FROM users WHERE id = #{id}")
    User findById(Integer id);

    // 根据用户名查询用户
    @Select("SELECT id, phone, password, name, avatar, created_at, updated_at " +
            "FROM users WHERE name = #{name}")
    User findByName(String name);

    // 更新用户密码
    @Update("UPDATE users SET password = #{newPassword}, updated_at = NOW() WHERE id = #{id}")
    void updatePassword(@Param("id") Integer id, @Param("newPassword") String newPassword);

    // 更新用户名
    @Update("UPDATE users SET name = #{newName}, updated_at = NOW() WHERE id = #{id}")
    void updateName(@Param("id") Integer id, @Param("newName") String newName);
    //更新用户头像
    @Update("update users set avatar=#{avatarUrl} where id=#{id}")
    void updateAvatar(String avatarUrl,Integer id);
}